//
//  ICMessageVController+Notification.m
//  ICome
//
//  Created by Administrator on 2016/10/25.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICMessageVController+Notification.h"
//#import "ICMessageVController+Logic.h"

#import "ICAction.h"
#import "ICSyncManager.h"
#import "ICNotify.h"
#import "ICMessageManager.h"
@implementation ICMessageVController (Notification)


#pragma mark - notification
- (void)initNotificationCenter {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interfaceReloadData:) name:NotificationChangeUIView object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(conversationCreated) name:NotificationDidCreatedConversation object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDataSource) name:NotificationReloadDataIMSource object:nil];
    
    //NotificationIMUnreadMessages
    // double
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadUnread) name:@"tabBarDidSelectedNotification" object:nil];
}

-(void)reloadUnread
{
    NSLog(@"reloadUnread");
    if(self.dataArray.count > 0){
        for(NSInteger i = 0; i < self.dataArray.count;i++)
        {
            ICGroup * group = self.dataArray[i];
            if(group.unReadCount > 0)
            {
                NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
                if(self.lastmessage != i && i >self.lastmessage){
                    self.lastmessage = i;
                    [[self tableView] scrollToRowAtIndexPath:scrollIndexPath
                                            atScrollPosition:UITableViewScrollPositionTop animated:YES];
                   return;
                }
            }
        }
        self.lastmessage = -1;
        for(NSInteger i = 0; i < self.dataArray.count;i++)
        {
            ICGroup * group = self.dataArray[i];
            if(group.unReadCount > 0)
            {
                NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
                if(self.lastmessage != i && i >self.lastmessage){
                    self.lastmessage = i;
                    [[self tableView] scrollToRowAtIndexPath:scrollIndexPath
                                            atScrollPosition:UITableViewScrollPositionTop animated:YES];
                    return;
                }
            }
        }
    }
}


#pragma mark - private

- (void)interfaceReloadData:(NSNotification *)notification {
    [self.tableView reloadData];
}


// 创建了新的会话
- (void)conversationCreated
{
    [self reloadDataSource];
}

// 刷新数据源
- (void)reloadDataSource
{
    WEAKSELF
    [[ICMessageManager instance] getMessageGroups:^(NSMutableArray *array) {
        [weakSelf.dataArray removeAllObjects];
        [weakSelf.dataArray addObjectsFromArray:[weakSelf filtersRbrGroups:array]];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableView reloadData];
            [weakSelf isRedCircleAppear];
        });
        if (weakSelf.dataArray.count > 0) {
            weakSelf.emptyView.hidden = YES;
        } else {
            weakSelf.emptyView.hidden = NO;
        }
    }];
}

// 对精灵小主助手处理 - 外层不显示 - 过滤掉
- (NSArray *)filtersRbrGroups:(NSArray *)array {
    int unreadCount = 0;
 
    NSMutableArray *filterList = [[NSMutableArray alloc] init];
    for (ICGroup *group in array) {
        if ([ICMessageHelper isServeRbtGroup:group.gId gType:group.gType]) { // 精灵组
            unreadCount = unreadCount + group.unReadCount;
        } else {
            [filterList addObject:group];
        }
    }
    
    NSString *svrId = [NSString stringWithFormat:@"svr_%@", [ICUser currentUser].eId];
    for (ICGroup *group  in filterList) {
        if ([group.gId isEqualToString:svrId]) {
            group.unReadCount = unreadCount;
        }
    }
   
    return filterList;
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
